﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GmPrizedrawTool
{
    public class BossEntityModel
    {
        /// <summary>
        /// BOSS ID
        /// </summary>
        public int ID { get; set; }
        /// <summary>
        /// BOSS 名字
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// BOSS 头像
        /// </summary>
        public string Avator { get; set; }
        /// <summary>
        /// BOSS 坐标
        /// </summary>
        public string Point { get; set; }
        /// <summary>
        /// 刷新间隔
        /// </summary>
        public int TimeSpan { get; set; }
        /// <summary>
        /// 开始时间
        /// </summary>
        public string StartTime { get; set; }
        /// <summary>
        /// 结束时间
        /// </summary>
        public string EndTime { get; set; }
        /// <summary>
        /// 刷新方式
        /// </summary>
        public string RefreshType { get; set; }
        /// <summary>
        /// 刷新喊话
        /// </summary>
        public string RefreshNotice { get; set; }
        /// <summary>
        /// 预告喊话
        /// </summary>
        public string YuGaoNotice { get; set; }
        /// <summary>
        /// 适配用户最低等级
        /// </summary>
        public int chrLevL { get; set; }
        /// <summary>
        /// 适配用户最高等级
        /// </summary>
        public int chrLevH { get; set; }
        /// <summary>
        /// 地图
        /// </summary>
        public string mapName { get; set; }
        /// <summary>
        /// 刷新概率
        /// </summary>
        public int refreshGL { get; set; }

        /// <summary>
        /// 加载Boss信息
        /// </summary>
        public static List<BossEntityModel> LoadBossInfo()
        {
            List<BossEntityModel> BossList = new List<BossEntityModel>();
            string filePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config\\allboss.csv");
            if (File.Exists(filePath))
            {
                DataTable BossCsvDt = CsvHelper.OpenCSV(filePath, 0, 0, 0, 0, false);
                if (BossCsvDt != null && BossCsvDt.Rows.Count > 0)
                {
                    for (int i = 0; i < BossCsvDt.Rows.Count; i++)
                    {
                        BossEntityModel model = new BossEntityModel();
                        DataRow row = BossCsvDt.Rows[i];
                        if (row["COL7"].ToString() == "xitong")
                        {
                            model.ID = Convert.ToInt32(row["COL0"]);
                            model.Name = row["COL1"].ToString();
                            model.Avator = row["COL2"].ToString();
                            model.Point = row["COL3"].ToString();
                            model.TimeSpan = Convert.ToInt32(row["COL4"]);
                            model.StartTime = row["COL5"].ToString();
                            model.EndTime = row["COL6"].ToString();
                            model.RefreshType = row["COL7"].ToString();
                            model.RefreshNotice = row["COL8"].ToString();
                            model.YuGaoNotice = row["COL9"].ToString();
                            string LevelStr = row["COL10"].ToString();
                            if (LevelStr.Contains("-"))
                            {
                                string[] levelArr = LevelStr.Split('-');
                                model.chrLevL = Convert.ToInt32(levelArr[0]);
                                model.chrLevH = Convert.ToInt32(levelArr[1]);
                            }
                            model.mapName = row["COL11"].ToString();
                            model.refreshGL = Convert.ToInt32(row["COL12"]);
                            BossList.Add(model);
                        }
                    }
                }
            }
            return BossList;
        }

        /// <summary>
        /// 通过Boss名字获取Boss信息
        /// </summary>
        /// <param name="BossName">Boss名字</param>
        /// <returns></returns>
        public static BossEntityModel GetModelByBossName(string BossName)
        {
            List<BossEntityModel> BossList = LoadBossInfo();
            return BossList.FirstOrDefault(p => p.Name.Contains(BossName));
        }

        /// <summary>
        /// 上传Boss被杀掉记录
        /// </summary>
        /// <param name="Id">Boss ID</param>
        /// <param name="timeSpan">刷新间隔</param>
        /// <returns></returns>
        public static bool SendBossKilledInfo(int Id, DateTimeOffset KillTime, int timeSpan)
        {
            string NoticeApiUrl = $"{Program.ApiAddress}Api/Interface/AddBossKilled";
            long unixTimestamp = KillTime.ToUnixTimeSeconds();
            string FormData = $"Id={Id}&KillTime={unixTimestamp}&ts={timeSpan}";
            string ReturnVal = CommonFun.HttpPostRaw(NoticeApiUrl, FormData);
            if (ReturnVal.Contains("200"))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 获取指定BOSS的下次刷新时间
        /// </summary>
        /// <param name="model">当前Boss信息</param>
        /// <returns></returns>
        public static DateTime NextRefreshTime(BossEntityModel model)
        {
            DateTime nowTime = DateTime.Now;
            if (model.RefreshType == "xitong" && !string.IsNullOrEmpty(model.StartTime))
            {
                int startHour = Convert.ToInt32(model.StartTime);
                DateTime startRefreshTime = Convert.ToDateTime($"{nowTime.Year}-{nowTime.Month}-{nowTime.Day} {startHour}:00:00");
                return startRefreshTime.AddDays(1);
            }
            else
            {
                return nowTime.AddSeconds(model.TimeSpan);
            }
        }

    }

    /// <summary>
    /// Boss 刷新预告实体
    /// </summary>
    public class BossRefreshNotice
    {
        /// <summary>
        /// 是否已预告刷新
        /// </summary>
        public bool HaveNotice { get; set; }
        /// <summary>
        /// 下次刷新时间
        /// </summary>
        public DateTime NextRefreshTime { get; set; }
        /// <summary>
        /// 预告喊话内容
        /// </summary>
        public string YgNotice { get; set; }
    }
}
